<template>
  <div >
    <div class="btn" @click="changeCollapse">|||</div>
    <el-menu text-color="#303133" 
      active-text-color="#409EFF" 
      unique-opened 
      :default-active="activePath"
      :collapse="isCollapse" 
      :collapse-transition='false'
      router>
        <el-submenu v-for="m in menulists" :key="m.id" :index="m.id + ''">
            <template slot="title">
                <i :class="iconObj[m.id]"></i>
                <span slot="title">{{m.authName}}</span>
            </template>
            <el-menu-item v-for="c in m.children" :key="c.id" :index=" c.path + ''" @click="saveActive(c.path + '')">
              <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{c.authName}}</span>
              </template>
            </el-menu-item>
        </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
    name:'Aside',
    created(){
      this.getMenuList(),
      this.activePath = sessionStorage.getItem('activePath')
    },
    data() {
      return {
        isCollapse: false,
        menulists:[],    //左侧菜单数据
        iconObj:{
          '125':'iconfont icon-users',
          '103':'iconfont icon-tijikongjian',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-danju',
          '145':'iconfont icon-baobiao',
        },    //字体图标
        activePath:''
      };
    },
    methods: {
      // 获取左侧菜单数据
      async getMenuList(){
        const res= await this.axios.get('menus');
        const {data} = res.data;
        // console.log(data);
        const {msg,status} = res.data.meta
        // console.log(data,msg,status);
        if(status !== 200) return this.$message.err(msg)
        this.menulists = data;
        // console.log(this.menulists);
      },
      changeCollapse(){
        this.$bus.$emit('getisCollapse',this.isCollapse)
        this.isCollapse = !this.isCollapse;
        // console.log(this.isCollapse);
        this.$bus.$emit('getisCollapse',this.isCollapse)
      },
      saveActive(str){
        sessionStorage.setItem('activePath',str);
        this.activePath = str
      }
    }
}
</script>

<style lang="less" scoped>
  .iconfont{
    margin-right: 10px;
  }
  .btn{
    background-color: #909399;
    font-size: 16px;
    line-height: 32px;
    color: #303133;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    user-select:none
  }
</style>